/**
 * @author harae Feng 
 * @date 2019-09-24 15:41:35 
 * @Last Modified by: harae Feng
 * @Last Modified time: 2019-10-22 16:01:09
 * @Describe 抖音-Dou听音乐榜
 */


const fetch = require('../../util/fetchUtil').fetchRequest
const ResultAPI = require('../../routes/api/resultAPI')
const CatchHandle = require('../../routes/api/apicatch')


/**
 * Dou听音乐榜-实时
 */
async function musicListenCurr(){
    let url = `https://www.iesdouyin.com/web/api/v2/hotsearch/billboard/music/`
    let res = await fetch(url)
    let resJSON = await res.json()
    return resJSON
}

/**
 * Dou听音乐榜-往期榜单
 * @param {String} edition_uid 时间周期榜单
 */
async function musicListenHistory(options){
    let url = ` https://api-hl.amemv.com/aweme/v1/hotsearch/music/billboard/?version_code=${options.version_code}&pass-region=${options['pass-region']}&pass-route=${options['pass-route']}&js_sdk_version=${options.js_sdk_version}&app_name=${options.app_name}&vid=${options.vid}&app_version=${options.app_version}&device_id=${options.device_id}&channel=${options.channel}&mcc_mnc=${options.mcc_mnc}&aid=${options.aid}&screen_width=${options.screen_width}&openudid=${options.openudid}&os_api=${options.os_api}&ac=${options.ac}&os_version=${options.os_version}&device_platform=${options.device_platform}&build_number=${options.build_number}&device_type=${options.device_type}&iid=${options.iid}&idfa=${options.idfa}&type=${options.type}&edition_uid=${options.edition_uid}&request_tag_from=${options.request_tag_from}`
    let res = await fetch(url)
    let resJSON = await res.json()
    return resJSON
}

async function musicListenCurrAPI(){
    try {
        let resJSON = await musicListenCurr()
        if(resJSON.status_code === 0){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003, {}, resJSON)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

async function musicListenHistoryAPI(ctx){
    try {
        const parms = ctx.query || {}
        if(!parms.edition_uid){
            return new ResultAPI(10000)
        }
        let __Options = Object.assign(parms, require('./__parms.js'))

        let resJSON = await musicListenHistory(__Options)
        if(resJSON.status_code === 0){
            return new ResultAPI(0, {}, resJSON)
        }else{
            return new ResultAPI(10003, {}, resJSON)
        }       
    } catch (error) {
        return CatchHandle(__dirname, __filename, error)
    }
}

module.exports = {
    musicListenCurrAPI: musicListenCurrAPI,
    musicListenHistoryAPI: musicListenHistoryAPI
}